package com.liz.lizrpccore.core.providers;

import com.alibaba.fastjson.JSON;
import com.liz.lizrpccore.entity.LizRpcArg;
import com.liz.lizrpccore.templates.MyRedisTemplate;
import com.liz.lizrpccore.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liangGongWei
 * @description
 * @create 2021-09-22 14:37
 **/
public class AbstractLizRpc {

    @Autowired
    private MyRedisTemplate myRedisTemplate;

    public MyRedisTemplate getMyRedisTemplate(){
        if (this.myRedisTemplate == null){
            myRedisTemplate = SpringContextUtils.getApplicationContext().getBean(MyRedisTemplate.class);
        }
        return myRedisTemplate;
    }

    protected List<Object> getArgs(List<LizRpcArg> argsList) {
        try {
            List<Object> res = new ArrayList<>();
            for (LizRpcArg arg : argsList) {
                Class<?> clazz = Class.forName(arg.getClassName());
                Object o = JSON.parseObject(arg.getObjStr(), clazz);
                res.add(o);
            }
            return res;
        } catch (Exception e) {
            throw new RuntimeException("参数转化异常：" + argsList);
        }
    }
}
